package cn.xudy.group.Controller;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.*;
import java.net.URLEncoder;

/**
 * Created by Ulegal on 2017/11/15.
 */
@RestController
public class MainController {

    @Autowired
    OSSClient ossClient;

    @PostMapping(value = "/upload")
    public String OSSUpLoad(@RequestParam(required = false)Part files){

        // 上传byte数组
//        byte[] content = "Hello OSS xudy".getBytes();
//        ossClient.putObject("xudy", "xudy", new ByteArrayInputStream(content));

        String filePath = System.getProperty("user.dir") + File.separator + "file" + File.separator + "top.jpg";
        // 上传文件流
        try {
            InputStream inputStream = files.getInputStream();
            File file = new File(filePath);
//            InputStream inputStream = new FileInputStream(file);

            // 上传文件
            ossClient.putObject("xudy", "txt.jpg", inputStream);
        } catch (Exception  e) {
//            e.printStackTrace();
            System.out.println("上传阿里云OSS服务器异常." + e.getMessage());

        }
       // 关闭client
//        ossClient.shutdown();

        return "upload";
    }

    @GetMapping(value = "/download")
    public void OssAction(HttpServletRequest request, HttpServletResponse response){
        try {

            String fileName="txt.jpg";
            String ossKey="txt.jpg";
            // 从阿里云进行下载
            OSSObject ossObject = ossClient.getObject("xudy",ossKey);//bucketName需要自己设置
            // 已缓冲的方式从字符输入流中读取文本，缓冲各个字符，从而提供字符、数组和行的高效读取
            BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent()));

            InputStream inputStream = ossObject.getObjectContent();

            //缓冲文件输出流
            BufferedOutputStream outputStream=new BufferedOutputStream(response.getOutputStream());
            //通知浏览器以附件形式下载
            response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(fileName,"UTF-8"));
            // 进行解码
            BASE64Decoder base64Decoder = new BASE64Decoder();
//            byte[] car;
//            while (true) {
//                String line = reader.readLine();
//                if (line == null) break;
//                car =  base64Decoder.decodeBuffer(line);
//
//                outputStream.write(car);
//            }
//            reader.close();

            byte[] car = new byte[1024];
                int L;

                while((L = inputStream.read(car)) != -1){
                    if (car.length!=0){
                        outputStream.write(car, 0,L);
                    }
                }

            if(outputStream!=null){
                outputStream.flush();
                outputStream.close();
            }


        } catch (IOException e) {
            e.printStackTrace();

        } catch (OSSException e){

        }
    }

    /**
     * 删除存储空间buckName
     */
    public  void deleteBucket(){
        ossClient.deleteBucket("");

    }

}
